import { _decorator, Component, Game, instantiate, Label, log, Node, Prefab, Vec3 } from 'cc';
import { Tower } from './Tower';
import { Bullet } from './Bullet';
import { ResourcesManager } from './ResourcesManager';
import { SoundManager } from './SoundManager';
const { ccclass, property } = _decorator;

//防御塔类型
enum TowerType {
    NONE,
    ARROW,
    BOMB
}

enum BULLET_TYPE {
    ARROW,
    BOMB
}

@ccclass('GameManager')
export class GameManager extends Component {

    //单例
    private static instance:GameManager = null;

    @property(Prefab)
    arrowBulletPrefab:Prefab = null;

    @property(Prefab)
    bombBulletPrefab:Prefab = null;

    @property(Node)
    bulletManager:Node = null;

    bulletTarget:Node = null;

    @property(Label)
    private enemyNumLabel:Label = null;

    @property(Label)
    private healthLabel:Label = null;

    @property(Label)
    private goldLabel:Label = null;

    private enemyNumber:number = 0;
    private health:number = 0;
    private gold:number = 0;

    soundManager:SoundManager = null;

    //获取实例
    public static getInstance():GameManager{
        return GameManager.instance;
    }

    //单例
    protected onLoad(): void {
        if(GameManager.instance == null){
            GameManager.instance = this;
        }
    }


    //游戏暂停设置
    

    start() {
        this.soundManager = SoundManager.instance;
        this.soundManager.playBgm();
        this.node.on("createBullet",this.createBullet,this);
        this.loadGameData();
    }

    update(deltaTime: number) {
        
    }


    //创建塔
    createTower(tower:Node):boolean{
        //根据塔的类型，扣除相应金币
        switch(tower.getComponent(Tower).type){
            //箭塔
            case TowerType.ARROW:
                if(this.gold < 50){
                    log("金币不足");
                    return false;
                }
                this.gold -= 50;
                break;

            //炮塔
            case TowerType.BOMB:
                if(this.gold < 70){
                    log("金币不足");
                    return false;
                }
                this.gold -= 70;
                break;
        }
        //更新金币
        this.goldLabel.string = this.gold.toString();
        return true;
    }

    //创建敌人
    createEnemy(){

    }

    /**
     * 创建子弹
     * @param tower 用于创建子弹的防御塔
     */
    createBullet(tower:Node,target:Node) {

        var towerComp = tower.getComponent(Tower);

        //根据传过来的塔node，创建相对应的子弹
        switch(towerComp.type){
            //箭塔子弹
            case TowerType.ARROW:

                if (!tower.isValid || !target.isValid) {
                    // newBullet.destroy();
                    return;
                }

                var newBullet = instantiate(this.arrowBulletPrefab);
                var bulletComp = newBullet.getComponent(Bullet);
                newBullet.setParent(tower);
                //初始化箭塔属性：目标，伤害，位置，类型
                bulletComp.init(target,bulletComp.damage,tower.children[0].worldPosition,BULLET_TYPE.ARROW);
                break;      

            //炮塔子弹
            case TowerType.BOMB:
                var newBullet = instantiate(this.bombBulletPrefab);
                var bulletComp = newBullet.getComponent(Bullet);
                //播放炮塔射炮动画
                towerComp.bombTowerReadtAni();

                setTimeout(() => {

                    // 在延迟后检查塔和目标是否仍然有效
                    if (!tower.isValid || !target.isValid) {
                        newBullet.destroy();
                        return;
                    }

                    newBullet.setParent(tower);
                    //初始化炸弹属性：目标，伤害，位置，类型
                    var x = tower.worldPosition.x;
                    //生产的炸弹刚好从炮塔 炮口出来
                    var y = tower.worldPosition.y+25;
                    bulletComp.init(target, bulletComp.damage, new Vec3(x,y,0), BULLET_TYPE.BOMB);
                }, 400);
                break;
        }
    }



    //加载游戏数据
    async loadGameData(){
        var data = await ResourcesManager.loadGameData("levelData/GameData");
        data = data.json.info[0];

        this.enemyNumber = data.enemyNumber;
        this.health = data.health;
        this.gold = data.gold;

        // this.enemyNumLabel.string = this.enemyNumber.toString();
        this.healthLabel.string = this.health.toString();
        this.goldLabel.string = this.gold.toString();
    }
}


